package com.jiyun.jiyunmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.jiyunmall.common.service.impl.CrudServiceImpl;
import com.jiyun.jiyunmall.product.dao.SkuInfoDao;
import com.jiyun.jiyunmall.product.dto.SkuInfoDTO;
import com.jiyun.jiyunmall.product.dto.SkuItemSaleAttrsVo;
import com.jiyun.jiyunmall.product.dto.SkuItemVo;
import com.jiyun.jiyunmall.product.dto.SpuItemAttrGroupVo;
import com.jiyun.jiyunmall.product.entity.SkuImagesEntity;
import com.jiyun.jiyunmall.product.entity.SkuInfoEntity;
import com.jiyun.jiyunmall.product.entity.SpuInfoDescEntity;
import com.jiyun.jiyunmall.product.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * sku信息
 *
 * @author boge 71164376@qq.com
 * @since 1.0.0 2022-07-12
 */
@Service
public class SkuInfoServiceImpl extends CrudServiceImpl<SkuInfoDao, SkuInfoEntity, SkuInfoDTO> implements SkuInfoService {

    @Override
    public QueryWrapper<SkuInfoEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Autowired
    SkuInfoDao skuInfoDao;
    @Autowired
    SkuImagesService imagesService;
    @Autowired
    SkuSaleAttrValueService saleAttrValueService;
    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    AttrGroupService attrGroupService;
    @Value("${oss.host}")
    String host;
    @Override
    public SkuItemVo item(Long skuId) {
        //最终要返回的类型对象，接下来的代码，就是各种查询各种表，然后将数据封装到skuItemVo
        SkuItemVo skuItemVo = new SkuItemVo();
        // 1、sku基本信息    pms_sku_info
        SkuInfoEntity skuInfo = skuInfoDao.selectById(skuId);
        if(skuInfo == null){
            return null;
        }
        skuInfo.setSkuDefaultImg(host+skuInfo.getSkuDefaultImg());
        skuItemVo.setInfo(skuInfo);
// 2、sku的图片信息   pms_sku_images
        List<SkuImagesEntity> images = imagesService.getImagesBySkuId(skuId);
        for (SkuImagesEntity image : images) {
            image.setImgUrl(host+image.getImgUrl());

        }
        skuItemVo.setImages(images);
        // 3、获取 spu 的销售属性组合
        Long spuId = skuInfo.getSpuId();
        //拿着spuId去查询，有哪些可选的销售属性值
        List<SkuItemSaleAttrsVo> saleAttrVos = saleAttrValueService.getSaleAttrsBySpuId(spuId);
        skuItemVo.setSaleAttr(saleAttrVos);
// 4、获取 spu 的介绍 pms_spu_info_desc
        SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.selectById(spuId);
        spuInfoDescEntity.setDecript(host+spuInfoDescEntity.getDecript());
        skuItemVo.setDesp(spuInfoDescEntity);
        Long catalogId = skuInfo.getCatalogId();
// 5、获取 spu 的规格参数信息 pms_spu_info_desc  基本属性
        List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        skuItemVo.setGroupAttrs(attrGroupVos);
        /*6、查询库存信息
            ware库.ware_sku
            1、编写wareFeign接口，根据skuid查询库存信息
            2、再将库存信息，放到skuItemVO
         */


        return skuItemVo;

    }
}